What is wrong with my shutdown routine?