loop termination fix