refactored the code. Added more meaningful comments