Fix shutdown code