This and the two lines above the normalize could use scratch parameters, but if we don't care about the performance of this function, I suppose it doesn't matter.