Made the calculation array-based instead of loop-based to increase performance.