Fixed several shutdown bugs