improved handling of loop exit