Fix some shutdown errors in certain circumstances