Moved r calculation and parallelized it