Written new unit tests and rewritten some existing ones, plus fixes revealed by these tests.