add comments and refactoring code