Added code to perform graceful shutdown.