Improve debugging helpers and clean up tests a bit.

Potpourri of changes that built up during #572.