some refactoring and cleanup