Replace copy&pasted code by iteration

Iterating over a list is a much better way to do repetitive tasks.