Corrected possible endless loop logic.