Bug fixes and unit tests.
More bug fixes and unit tests.