added before_exit function