Refactoring, adding tests