diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index 6a1a5546c7c3ef900667f9a04a89a221cdccfc44..6298660268f381b5b89531ef187b2778941c5757 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -2122,9 +2122,15 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
                                     julia ? oJuliaDynamicModel : oMatlabDynamicModel);
 
   deriv_node_temp_terms_t tef_terms;
+  temporary_terms_t temp_terms;
+  if (julia)
+    temp_terms = temporary_terms_res;
+  else
+    temp_terms = temporary_terms;
+
   writeModelLocalVariables(model_local_vars_output, output_type, tef_terms);
 
-  writeTemporaryTerms(temporary_terms_res, model_output, output_type, tef_terms);
+  writeTemporaryTerms(temp_terms, model_output, output_type, tef_terms);
 
   writeModelEquations(model_output, output_type);
 
@@ -2132,7 +2138,12 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
   int hessianColsNbr = dynJacobianColsNbr * dynJacobianColsNbr;
 
   // Writing Jacobian
-  writeTemporaryTerms(temporary_terms_g1, jacobian_output, output_type, tef_terms);
+  if (julia)
+    {
+      temp_terms = temporary_terms_g1;
+      if (!first_derivatives.empty())
+      writeTemporaryTerms(temp_terms, jacobian_output, output_type, tef_terms);
+    }
   for (first_derivatives_t::const_iterator it = first_derivatives.begin();
        it != first_derivatives.end(); it++)
     {
@@ -2142,13 +2153,17 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
 
       jacobianHelper(jacobian_output, eq, getDynJacobianCol(var), output_type);
       jacobian_output << "=";
-      d1->writeOutput(jacobian_output, output_type, temporary_terms_g1, tef_terms);
+      d1->writeOutput(jacobian_output, output_type, temp_terms, tef_terms);
       jacobian_output << ";" << endl;
     }
 
   // Writing Hessian
-  if (second_derivatives.size() > 0)
-    writeTemporaryTerms(temporary_terms_g2, hessian_output, output_type, tef_terms);
+  if (julia)
+    {
+      temp_terms = temporary_terms_g2;
+      if (!second_derivatives.empty())
+        writeTemporaryTerms(temp_terms, hessian_output, output_type, tef_terms);
+    }
   int k = 0; // Keep the line of a 2nd derivative in v2
   for (second_derivatives_t::const_iterator it = second_derivatives.begin();
        it != second_derivatives.end(); it++)
@@ -2169,7 +2184,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
         {
           for_sym << "g2[" << eq + 1 << "," << col_nb + 1 << "]";
           hessian_output << "  @inbounds " << for_sym.str() << " = ";
-          d2->writeOutput(hessian_output, output_type, temporary_terms_g2, tef_terms);
+          d2->writeOutput(hessian_output, output_type, temp_terms, tef_terms);
           hessian_output << endl;
         }
       else
@@ -2182,7 +2197,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
 
           sparseHelper(2, hessian_output, k, 2, output_type);
           hessian_output << "=";
-          d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms);
+          d2->writeOutput(hessian_output, output_type, temp_terms, tef_terms);
           hessian_output << ";" << endl;
 
           k++;
@@ -2211,8 +2226,12 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
     }
 
   // Writing third derivatives
-  if (third_derivatives.size() > 0)
-    writeTemporaryTerms(temporary_terms_g3, third_derivatives_output, output_type, tef_terms);
+  if (julia)
+    {
+      temp_terms = temporary_terms_g3;
+      if (!third_derivatives.empty())
+        writeTemporaryTerms(temp_terms, third_derivatives_output, output_type, tef_terms);
+    }
   k = 0; // Keep the line of a 3rd derivative in v3
   for (third_derivatives_t::const_iterator it = third_derivatives.begin();
        it != third_derivatives.end(); it++)
@@ -2235,7 +2254,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
         {
           for_sym << "g3[" << eq + 1 << "," << ref_col + 1 << "]";
           third_derivatives_output << "  @inbounds " << for_sym.str() << " = ";
-          d3->writeOutput(third_derivatives_output, output_type, temporary_terms_g3, tef_terms);
+          d3->writeOutput(third_derivatives_output, output_type, temp_terms, tef_terms);
           third_derivatives_output << endl;
         }
       else
@@ -2248,7 +2267,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
 
           sparseHelper(3, third_derivatives_output, k, 2, output_type);
           third_derivatives_output << "=";
-          d3->writeOutput(third_derivatives_output, output_type, temporary_terms, tef_terms);
+          d3->writeOutput(third_derivatives_output, output_type, temp_terms, tef_terms);
           third_derivatives_output << ";" << endl;
         }