Minor performance improvement by removing inverse trig calls in a loop and remove more temporary object creation.