Added many more tests, and a few resulting bug fixes.