Replace copy-paste lines with loop