fixed bug in loop