added unit tests, and minor bug fixes