added comments and refactored