Refactored the code for readability