some refactoring and cleaning