Change some of the blocks of cut&pasted code into loops.