Correct order of exit function calls