added tests, refactored