diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index d6989d37cc9e1811bb614f4db0974f6c99af0df9..06dc7a5bc2222e4a3327c8c363710c2793817aed 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -5874,13 +5874,13 @@ DynamicModel::substituteUnaryOps(StaticModel &static_model, vector<int> &eqnumbe
   for (auto & it : local_variables_table)
     it.second = it.second->substituteUnaryOpNodes(static_model, nodes, subst_table, neweqs);
 
-  // Substitute in equations
-  for (auto & equation : equations)
+  // Substitute only in equations requested
+  for (int eqnumber : eqnumbers)
     {
-      auto *substeq = dynamic_cast<BinaryOpNode *>(equation->
+      auto *substeq = dynamic_cast<BinaryOpNode *>(equations[eqnumber]->
                                                    substituteUnaryOpNodes(static_model, nodes, subst_table, neweqs));
       assert(substeq != nullptr);
-      equation = substeq;
+      equations[eqnumber] = substeq;
     }
 
   // Add new equations