Added unit tests with some fixes