Could the above code be enclosed in a loop? It seems lines 188 to 210 do the same twice.