refactor using reverse_each loop to achieve same result