Added failsafes for cleanup code.