Adding some tests and fixing a bug revealed by the tests.