sped up computation of M and r with an if-statement, MUCH faster