some refactoring and minor changes