fixed the loop condition