inversion and some refactoring