added unit tests and corrected several errors