Maybe closes #638 by allowing the cleanup code to be executed cleanly