Fixed shutdown code