refactored, added tests