Added unit tests for api and bug fixes for bugs revealed by testing.