diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 3a5f78a1e65ff579640932c648c0e0cd9ca2333a..1cc250a1552a675fe6bd4690e5d9ecf1051b6a65 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -3455,6 +3455,37 @@ DynamicModel::runTrendTest(const eval_context_t &eval_context)
   testTrendDerivativesEqualToZero(eval_context);
 }
 
+void
+DynamicModel::updateVarAndTrendModelRhs() const
+{
+  for (int i = 0; i < 2; i++)
+    {
+      map<string, vector<int>> eqnums;
+      if (i == 0)
+        eqnums = var_model_table.getEqNums();
+      else if (i == 1)
+        eqnums = trend_component_model_table.getEqNums();
+
+      map<string, vector<set<pair<int, int>>>> rhsr;
+      for (const auto & it : eqnums)
+        {
+          vector<set<pair<int, int>>> rhs;
+          for (auto eqn : it.second)
+            {
+              set<pair<int, int>> rhs_set;
+              equations[eqn]->get_arg2()->collectDynamicVariables(SymbolType::endogenous, rhs_set);
+              rhs.push_back(rhs_set);
+            }
+          rhsr[it.first] = rhs;
+        }
+
+      if (i == 0)
+        var_model_table.setRhs(rhsr);
+      else if (i == 1)
+        trend_component_model_table.setRhs(rhsr);
+    }
+}
+
 void
 DynamicModel::fillVarModelTable() const
 {
diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh
index da8d6397d33f737fbe3f708c2a47fc959c913846..30bdc162037c800faa023922aeb12660a4012408 100644
--- a/src/DynamicModel.hh
+++ b/src/DynamicModel.hh
@@ -312,6 +312,10 @@ public:
   void fillVarModelTable() const;
   void fillVarModelTableFromOrigModel(StaticModel &static_model) const;
 
+  //! Update the rhs references in the var model and trend component tables
+  //! after substitution of auxiliary variables
+  void updateVarAndTrendModelRhs() const;
+
   //! Add aux equations (and aux variables) for variables declared in var_model
   //! at max order if they don't already exist
   void addEquationsForVar();
diff --git a/src/ModFile.cc b/src/ModFile.cc
index 70e2eb65a385973ba1e06c257f97f1d0c03a65e0..77a071dcd0449e5594f45a1a69adebb3ac4f311d 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -580,8 +580,7 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, const
       dynamic_model.substituteEndoLagGreaterThanTwo(true);
     }
 
-  dynamic_model.fillVarModelTable();
-  dynamic_model.fillTrendComponentModelTable();
+  dynamic_model.updateVarAndTrendModelRhs();
 
   if (differentiate_forward_vars)
     dynamic_model.differentiateForwardVars(differentiate_forward_vars_subset);