Added comments and refactored