Handle shutdown more gracefully.