refactor, added unit tests