fixed on_exit function