added comments and refactoring