Put major part of the arithmetic inside a loop