more refactoring, added unit tests