added comments and refactored code