Inlined some functions. Optimized calcAngle with a try-catch.