adding comments and refactoring

adding comments and refactoring