Add lots of tests and fix bugs identified by these tests.