diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index e69244eecd8acad91e00ef074124ff8b51f73f8e..a41e7b7a89a82eed0c2cfed17159a40b2910a6ff 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -2318,10 +2318,10 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
                     << "  %" << endl
                     << endl
                     << jacobian_output.str()
-                    << "end" << endl;
+                    << endl
 
       // Initialize g2 matrix
-      DynamicOutput << "if nargout >= 3," << endl
+                    << "if nargout >= 3," << endl
                     << "  %" << endl
                     << "  % Hessian matrix" << endl
                     << "  %" << endl
@@ -2332,7 +2332,6 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
                       << "  g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << nrows << "," << hessianColsNbr << ");" << endl;
       else // Either hessian is all zero, or we didn't compute it
         DynamicOutput << "  g2 = sparse([],[],[]," << nrows << "," << hessianColsNbr << ");" << endl;
-      DynamicOutput << "end" << endl;
 
       // Initialize g3 matrix
       DynamicOutput << "if nargout >= 4," << endl
@@ -2348,7 +2347,9 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
       else // Either 3rd derivatives is all zero, or we didn't compute it
         DynamicOutput << "  g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl;
 
-      DynamicOutput << "end" << endl;
+      DynamicOutput << "end" << endl
+                    << "end" << endl
+                    << "end" << endl;
     }
   else if (output_type == oCDynamicModel)
     {
@@ -2362,28 +2363,25 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia
                     << "  /* Jacobian  */" << endl
                     << "  if (g1 == NULL)" << endl
                     << "    return;" << endl
-                    << "  else" << endl
-                    << "    {" << endl
+                    << endl
                     << jacobian_output.str()
-                    << "    }" << endl;
+                    << endl;
 
       if (second_derivatives.size())
         DynamicOutput << "  /* Hessian for endogenous and exogenous variables */" << endl
                       << "  if (v2 == NULL)" << endl
                       << "    return;" << endl
-                      << "  else" << endl
-                      << "    {" << endl
+                      << endl
                       << hessian_output.str()
-                      << "    }" << endl;
+                      << endl;
 
       if (third_derivatives.size())
         DynamicOutput << "  /* Third derivatives for endogenous and exogenous variables */" << endl
                       << "  if (v3 == NULL)" << endl
                       << "    return;" << endl
-                      << "  else" << endl
-                      << "    {" << endl
+                      << endl
                       << third_derivatives_output.str()
-                      << "    }" << endl;
+                      << endl;
 
       DynamicOutput << "}" << endl << endl;
     }
diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc
index 81b16761ea2b0af620df3ee880fc3f963c65c20f..92aa43f64460acab701f03cbb3db4a52fac7f0a4 100644
--- a/preprocessor/StaticModel.cc
+++ b/preprocessor/StaticModel.cc
@@ -1392,7 +1392,6 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
                    << "  if ~isreal(g1)" << endl
                    << "    g1 = real(g1)+2*imag(g1);" << endl
                    << "  end" << endl
-                   << "end" << endl
                    << "if nargout >= 3," << endl
                    << "  %" << endl
                    << "  % Hessian matrix" << endl
@@ -1405,7 +1404,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
                      << "  g2 = sparse(v2(:,1),v2(:,2),v2(:,3)," << equations.size() << "," << g2ncols << ");" << endl;
       else
         StaticOutput << "  g2 = sparse([],[],[]," << equations.size() << "," << g2ncols << ");" << endl;
-      StaticOutput << "end" << endl;
+
       // Initialize g3 matrix
       StaticOutput << "if nargout >= 4," << endl
                     << "  %" << endl
@@ -1419,6 +1418,9 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
                       << "  g3 = sparse(v3(:,1),v3(:,2),v3(:,3)," << nrows << "," << ncols << ");" << endl;
       else // Either 3rd derivatives is all zero, or we didn't compute it
         StaticOutput << "  g3 = sparse([],[],[]," << nrows << "," << ncols << ");" << endl;
+      StaticOutput << "end" << endl
+                   << "end" << endl
+                   << "end" << endl;
     }
   else if (output_type == oCStaticModel)
     {
@@ -1432,27 +1434,24 @@ StaticModel::writeStaticModel(ostream &StaticOutput, bool use_dll, bool julia) c
                    << "  /* Jacobian  */" << endl
                    << "  if (g1 == NULL)" << endl
                    << "    return;" << endl
-                   << "  else" << endl
-                   << "    {" << endl
+                   << endl
                    << jacobian_output.str()
-                   << "    }" << endl;
+                   << endl;
 
       if (second_derivatives.size())
         StaticOutput << "  /* Hessian for endogenous and exogenous variables */" << endl
                      << "  if (v2 == NULL)" << endl
                      << "    return;" << endl
-                     << "  else" << endl
-                     << "    {" << endl
+                     << endl
                      << hessian_output.str()
-                     << "    }" << endl;
+                     << endl;
       if (third_derivatives.size())
         StaticOutput << "  /* Third derivatives for endogenous and exogenous variables */" << endl
-                      << "  if (v3 == NULL)" << endl
-                      << "    return;" << endl
-                      << "  else" << endl
-                      << "    {" << endl
-                      << third_derivatives_output.str()
-                      << "    }" << endl;
+                     << "  if (v3 == NULL)" << endl
+                     << "    return;" << endl
+                     << endl
+                     << third_derivatives_output.str()
+                     << endl;
     }
   else
     {