added unit tests and refactored