Some cleanup work

This is an experimental fix for #481. Comments welcome.