fix a bug in the loop condition