add some comments and refactored