added tests and refactored a bit