added unit tests, refactored