Made a repeatedly used variable a local variable of the function instead of the for loop