fixed bug in loop check