Refactoring: old style cast replaced, pow(x, 2) replaced with sqr(x)=x*x.