Fixed bugs when exiting a process