Add tests, fix bugs.
More tests and fixes.