simplify the looping logic