refactored the code to format the response