changed logic to run at_exit hook only once per initialization