diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index eccc4e9c5aa38f09e4f84c93ee7c847ce7ff2041..de35ef8d4fd57822c68b8534e15d4f2130dd08c1 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -4018,8 +4018,14 @@ DynamicModel::substituteLeadLagInternal(aux_var_t type, bool deterministic_model
   for (int i = 0; i < (int) neweqs.size(); i++)
     addEquation(neweqs[i]);
 
-  // Add the new set of equations at the *beginning* of aux_equations
-  copy(neweqs.rbegin(), neweqs.rend(), front_inserter(aux_equations));
+  // Order of auxiliary variable definition equations:
+  //  - expectation (entered before this function is called)
+  //  - lead variables from lower lead to higher lead (need to be listed in reverse order)
+  //  - lag variables from lower lag to higher lag
+  if ((type == avEndoLead) || (type == avExoLead))
+    copy(neweqs.rbegin(), neweqs.rend(), back_inserter(aux_equations));
+  else
+    copy(neweqs.begin(), neweqs.end(), back_inserter(aux_equations));
 
   if (neweqs.size() > 0)
     {
diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc
index adc84c23ee64d2fa86593217daf662ac580243fa..2b8c158422a133bbd047cc9dab0262162c24f9c1 100644
--- a/preprocessor/StaticModel.cc
+++ b/preprocessor/StaticModel.cc
@@ -1702,7 +1702,7 @@ StaticModel::writeLatexFile(const string &basename) const
 void
 StaticModel::writeAuxVarInitval(ostream &output, ExprNodeOutputType output_type) const
 {
-  for (int i = (int) aux_equations.size()-1; i >= 0; i--)
+  for (int i = 0; i < (int) aux_equations.size(); i++)
     {
       dynamic_cast<ExprNode *>(aux_equations[i])->writeOutput(output, output_type);
       output << ";" << endl;
@@ -1730,7 +1730,7 @@ void StaticModel::writeAuxVarRecursiveDefinitions(const string &basename) const
          << "%           from model file (.mod)" << endl
          << endl;
 
-  for (int i = (int) aux_equations.size()-1; i >= 0; i--)
+  for (int i = 0; i < (int) aux_equations.size(); i++)
     {
       dynamic_cast<ExprNode *>(aux_equations[i])->writeOutput(output, oMatlabStaticModel);
       output << ";" << endl;