added unit tests, fixed problems that were exposed