Properly handle shutdown.