Refactor this to use a loop. Lots of cut and pasted code.