fixed looping logic