Implemented graceful shutdown