added tests, simple refactoring