Added more unit tests. fixed issues discovered from the tests.