some refactoring and tests