Fix graceful shutdown