some refactoring and fixing broken tests