fixed bug in the loop