Bug fixes and add tests.
Bug fixes and add tests.