Added unit tests and fixed discovered errors