Added unit tests and fixed errors