Bug fix, and some tests.