refactoring, added unit tests