added tests
some refactoring