small optimisation, calculations outside loop