More tests and minor code cleanups.