Refactoring, added tests