Improved shutdown handling to detect if shutdown was requested from notebook.