Index: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/CVODES.cs
===================================================================
--- branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/CVODES.cs	(revision 16251)
+++ branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/CVODES.cs	(revision 16253)
@@ -364,5 +364,5 @@
     [DllImport("sundials_cvodes.dll", EntryPoint = "CVodeFree", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
 
-    public static extern int CVodeFree(IntPtr cvode_mem);
+    public static extern int CVodeFree(ref IntPtr cvode_mem);
 
     #region matrix
Index: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs
===================================================================
--- branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs	(revision 16251)
+++ branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs	(revision 16253)
@@ -537,33 +537,5 @@
 
     // for a solver with the necessary features see: https://computation.llnl.gov/projects/sundials/cvodes
-    /*
-     * SUNDIALS: SUite of Nonlinear and DIfferential/ALgebraic Equation Solvers
-     * CVODES
-     * CVODES is a solver for stiff and nonstiff ODE systems (initial value problem) given in explicit 
-     * form y’ = f(t,y,p) with sensitivity analysis capabilities (both forward and adjoint modes). CVODES
-     * is a superset of CVODE and hence all options available to CVODE (with the exception of the FCVODE 
-     * interface module) are also available for CVODES. Both integration methods (Adams-Moulton and BDF) 
-     * and the corresponding nonlinear iteration methods, as well as all linear solver and preconditioner
-     * modules, are available for the integration of the original ODEs, the sensitivity systems, or the 
-     * adjoint system. Depending on the number of model parameters and the number of functional outputs, 
-     * one of two sensitivity methods is more appropriate. The forward sensitivity analysis (FSA) method 
-     * is mostly suitable when the gradients of many outputs (for example the entire solution vector) with 
-     * respect to relatively few parameters are needed. In this approach, the model is differentiated with 
-     * respect to each parameter in turn to yield an additional system of the same size as the original 
-     * one, the result of which is the solution sensitivity. The gradient of any output function depending
-     * on the solution can then be directly obtained from these sensitivities by applying the chain rule
-     * of differentiation. The adjoint sensitivity analysis (ASA) method is more practical than the 
-     * forward approach when the number of parameters is large and the gradients of only few output 
-     * functionals are needed. In this approach, the solution sensitivities need not be computed 
-     * explicitly. Instead, for each output functional of interest, an additional system, adjoint to the 
-     * original one, is formed and solved. The solution of the adjoint system can then be used to evaluate
-     * the gradient of the output functional with respect to any set of model parameters. The FSA module 
-     * in CVODES implements a simultaneous corrector method as well as two flavors of staggered corrector
-     * methods–for the case when sensitivity right hand sides are generated all at once or separated for
-     * each model parameter. The ASA module provides the infrastructure required for the backward 
-     * integration in time of systems of differential equations dependent on the solution of the original
-     * ODEs. It employs a checkpointing scheme for efficient interpolation of forward solutions during the
-     * backward integration.
-     */
+
     private static IEnumerable<Tuple<double, Vector>[]> Integrate(
       ISymbolicExpressionTree[] trees, IDataset dataset, string[] inputVariables, string[] targetVariables, string[] latentVariables, IEnumerable<IntRange> episodes,
@@ -596,10 +568,12 @@
         var calculatedVariables = targetVariables.Concat(latentVariables).ToArray(); // TODO: must conincide with the order of trees in the encoding
 
+        var prevT = rows.First(); // TODO: here we should use a variable for t if it is available. Right now we assume equidistant measurements.
         foreach (var t in rows.Skip(1)) {
           if (odeSolver == "HeuristicLab")
             IntegrateHL(trees, calculatedVariables, variableValues, parameterValues, numericIntegrationSteps);
           else if (odeSolver == "CVODES")
-            IntegrateCVODES(trees, calculatedVariables, variableValues, parameterValues, t);
+            IntegrateCVODES(trees, calculatedVariables, variableValues, parameterValues, t - prevT);
           else throw new InvalidOperationException("Unknown ODE solver " + odeSolver);
+          prevT = t;
 
           if (variableValues.Count == targetVariables.Length) {
@@ -623,4 +597,13 @@
     }
 
+
+    /// <summary>
+    ///  Here we use CVODES to solve the ODE. Forward sensitivities are used to calculate the gradient for parameter optimization
+    /// </summary>
+    /// <param name="trees">Each equation in the ODE represented as a tree</param>
+    /// <param name="calculatedVariables">The names of the calculated variables</param>
+    /// <param name="variableValues">The start values of the calculated variables as well as their sensitivites over parameters</param>
+    /// <param name="parameterValues">The current parameter values</param>
+    /// <param name="t">The time t up to which we need to integrate.</param>
     private static void IntegrateCVODES(
       ISymbolicExpressionTree[] trees, // f(y,p) in tree representation
@@ -725,5 +708,5 @@
       } finally {
         if (y != IntPtr.Zero) CVODES.N_VDestroy_Serial(y);
-        if (cvode_mem != IntPtr.Zero) CVODES.CVodeFree(cvode_mem);
+        if (cvode_mem != IntPtr.Zero) CVODES.CVodeFree(ref cvode_mem);
         if (linearSolver != IntPtr.Zero) CVODES.SUNLinSolFree(linearSolver);
         if (A != IntPtr.Zero) CVODES.SUNMatDestroy(A);
