Fix shutdown sequence