Add some tests and fix some minor bugs.