abstracted loop code