Removing calculation of distmax^2 outside of loop