Converted r (in step 2) and a (in step 3) to longs.
Removed sqrt function (shouldn't be necessary now).
Moved step 3 to its own function