Added try/catch around shutdown code for more graceful error recovery.