Optimized math and added memoization to inner loops core routines