some refactoring and add all comments