Fix the unit tests and some bugs they found.