How can i improve this code to meet the requirements