Add some tests, fix a few bugs.