Fixes for two hairy test environment cleanup bugs

These fix all the failures uncovered in #889