extract the same iteration logic to utils