added tests and refactored