More unit tests and bug fixes.