Completed tests and a few test-inspired bugfixes.