Fixed crashes when exiting