Added comments for code review. Refactored code and added comments for clarity.