added comments and refactored
added comments