Added code to handle shutdown.