More bug fixes and tests.