move summation outside the loop