Vectorized main calculation, removed rendundancy