General cleanup, more tests, and even a bug fix.