Adding cache for the calculation of the curves and their derivatives. Refactorising.