Added code to shutdown gracefully