corrected loop condition