Adds graceful shutdown functionality.