Added comments and refactored code.