formatting and some refactoring