Improving performance

Moving math.sqrt usage out of the loop.