some refactoring with added tests