some refactoring and comments