added unit tests, fixed bugs