some refactoring and shuffling