Fix test bugs

This fixes bugs in many tests, and a couple of actual bugs that the tests found.
We should have pretty good confidence in our testing procedure with this in place.
I anticipate 2 failures only.