diff --git a/preprocessor/DataTree.cc b/preprocessor/DataTree.cc
index cc8da091d28b990080576c94986a088f0a2fc853..f151fa3aa8be1e22c474189764e60c0a12a19587 100644
--- a/preprocessor/DataTree.cc
+++ b/preprocessor/DataTree.cc
@@ -659,6 +659,7 @@ DataTree::writePowerDeriv(ostream &output, bool use_dll) const
            << "            dxp = dxp*p;" << endl
            << "            p = p-1;" << endl
            << "        end" << endl
-           << "    end" << endl;
+           << "    end" << endl
+           << "end" << endl;
 
 }
diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index ee0673ec6d5a58c51a7f2febbf37a65c5846b74b..ec81d22930d94e2dd9028530d01a4d1cb806d5ea 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -1530,6 +1530,7 @@ DynamicModel::writeDynamicMFile(const string &dynamic_basename) const
     mDynamicModelFile << "global oo_;" << endl << endl;
 
   writeDynamicModel(mDynamicModelFile, false);
+  mDynamicModelFile << "end" << endl; // Close *_dynamic function
   writePowerDeriv(mDynamicModelFile, false);
   mDynamicModelFile.close();
 }
@@ -2213,7 +2214,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const
                       << "  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;
+      DynamicOutput << "end" << endl;
 
       // Initialize g3 matrix
       DynamicOutput << "if nargout >= 4," << endl
@@ -2229,7 +2230,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const
       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;
     }
   else
     {
diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc
index f072ff2b26be8ffb9f28ea24b29fb47a7c2824be..79624b3f6da82ff34a26c8704a30d9870a8d3148 100644
--- a/preprocessor/StaticModel.cc
+++ b/preprocessor/StaticModel.cc
@@ -1244,7 +1244,8 @@ StaticModel::writeStaticMFile(const string &func_name) const
   else // Either hessian is all zero, or we didn't compute it
     output << "  g2 = sparse([],[],[]," << equations.size() << "," << g2ncols << ");" << endl;
 
-  output << "end;" << endl; // Close the if nargout >= 3 statement
+  output << "end" << endl; // Close the if nargout >= 3 statement
+  output << "end" << endl; // Close the *_static function
   writePowerDeriv(output, false);
   output.close();
 }