Speed up the calculation with some restructuring