some refactoring and cleaning up