Fix some issues with graceful shutdown