diff --git a/ExprNode.cc b/ExprNode.cc
index 06a8a69129741f33488ef726aef6a16ebbe711c2..eb4b6364b83d4504a308442f9476147cd538566d 100644
--- a/ExprNode.cc
+++ b/ExprNode.cc
@@ -73,18 +73,6 @@ ExprNode::cost(const temporary_terms_type &temporary_terms, bool is_matlab) cons
   return 0;
 }
 
-int
-ExprNode::present_endogenous_size() const
-{
-  return(present_endogenous.size());
-}
-
-int
-ExprNode::present_endogenous_find(int var, int lag) const
-{
-  return(present_endogenous.find(make_pair(var,lag))!=present_endogenous.end());
-}
-
 void
 ExprNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
                                 temporary_terms_type &temporary_terms,
@@ -172,7 +160,7 @@ NumConstNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou
 
 
 void
-NumConstNode::collectEndogenous(NodeID &Id)
+NumConstNode::collectEndogenous(set<pair<int, int> > &result) const
 {
 }
 
@@ -479,10 +467,10 @@ VariableNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType ou
 }
 
 void
-VariableNode::collectEndogenous(NodeID &Id)
+VariableNode::collectEndogenous(set<pair<int, int> > &result) const
 {
   if (type == eEndogenous)
-    Id->present_endogenous.insert(make_pair(symb_id, lag));
+    result.insert(make_pair(symb_id, lag));
 }
 
 UnaryOpNode::UnaryOpNode(DataTree &datatree_arg, UnaryOpcode op_code_arg, const NodeID arg_arg) :
@@ -871,9 +859,9 @@ UnaryOpNode::compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType out
 }
 
 void
-UnaryOpNode::collectEndogenous(NodeID &Id)
+UnaryOpNode::collectEndogenous(set<pair<int, int> > &result) const
 {
-  arg->collectEndogenous(Id);
+  arg->collectEndogenous(result);
 }
 
 BinaryOpNode::BinaryOpNode(DataTree &datatree_arg, const NodeID arg1_arg,
@@ -1328,10 +1316,10 @@ BinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
 }
 
 void
-BinaryOpNode::collectEndogenous(NodeID &Id)
+BinaryOpNode::collectEndogenous(set<pair<int, int> > &result) const
 {
-  arg1->collectEndogenous(Id);
-  arg2->collectEndogenous(Id);
+  arg1->collectEndogenous(result);
+  arg2->collectEndogenous(result);
 }
 
 TrinaryOpNode::TrinaryOpNode(DataTree &datatree_arg, const NodeID arg1_arg,
@@ -1595,11 +1583,11 @@ TrinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
 }
 
 void
-TrinaryOpNode::collectEndogenous(NodeID &Id)
+TrinaryOpNode::collectEndogenous(set<pair<int, int> > &result) const
 {
-  arg1->collectEndogenous(Id);
-  arg2->collectEndogenous(Id);
-  arg3->collectEndogenous(Id);
+  arg1->collectEndogenous(result);
+  arg2->collectEndogenous(result);
+  arg3->collectEndogenous(result);
 }
 
 UnknownFunctionNode::UnknownFunctionNode(DataTree &datatree_arg,
@@ -1655,10 +1643,11 @@ UnknownFunctionNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
 }
 
 void
-UnknownFunctionNode::collectEndogenous(NodeID &Id)
+UnknownFunctionNode::collectEndogenous(set<pair<int, int> > &result) const
 {
-  cerr << "UnknownFunctionNode::collectEndogenous: not implemented" << endl;
-  exit(-1);
+  for(vector<NodeID>::const_iterator it = arguments.begin();
+      it != arguments.end(); it++)
+    (*it)->collectEndogenous(result);
 }
 
 double
diff --git a/ModelTree.cc b/ModelTree.cc
index 08a96a8719b8a1e9e78bc94a5a1ca0441b48f5a2..8c76a171be4b11992e910b7d742f851cab4c6e26 100644
--- a/ModelTree.cc
+++ b/ModelTree.cc
@@ -3647,12 +3647,13 @@ ModelTree::BlockLinear(Model_Block *ModelBlock)
               if (it!= first_derivatives.end())
                 {
                   NodeID Id = it->second;
-                  Id->collectEndogenous(Id);
-                  if (Id->present_endogenous_size()>0)
+                  set<pair<int, int> > endogenous;
+                  Id->collectEndogenous(endogenous);
+                  if (endogenous.size() > 0)
                     {
                       for(l=0;l<ModelBlock->Block_List[j].Size;l++)
                         {
-                          if (Id->present_endogenous_find(ModelBlock->Block_List[j].Variable[l],0))
+                          if (endogenous.find(make_pair(ModelBlock->Block_List[j].Variable[l], 0)) != endogenous.end())
                             {
                               ModelBlock->Block_List[j].is_linear=false;
                               goto follow;
@@ -3675,12 +3676,13 @@ ModelTree::BlockLinear(Model_Block *ModelBlock)
                   NodeID Id = it->second;
                   if (it!= first_derivatives.end())
                     {
-                      Id->collectEndogenous(Id);
-                      if (Id->present_endogenous_size()>0)
+                      set<pair<int, int> > endogenous;
+                      Id->collectEndogenous(endogenous);
+                      if (endogenous.size() > 0)
                         {
                           for(l=0;l<ModelBlock->Block_List[j].Size;l++)
                             {
-                              if (Id->present_endogenous_find(ModelBlock->Block_List[j].Variable[l],k1))
+                              if (endogenous.find(make_pair(ModelBlock->Block_List[j].Variable[l], k1)) != endogenous.end())
                                 {
                                   ModelBlock->Block_List[j].is_linear=false;
                                   goto follow;
diff --git a/include/ExprNode.hh b/include/ExprNode.hh
index c3779c4e92441329de0834391ce53d5e5b6d3e3e..2e8fb3bb5e7f1a998bad4b767733587563973e92 100644
--- a/include/ExprNode.hh
+++ b/include/ExprNode.hh
@@ -116,10 +116,6 @@ protected:
   /*! Nodes included in temporary_terms are considered having a null cost */
   virtual int cost(const temporary_terms_type &temporary_terms, bool is_matlab) const;
 
-  //! set of endogenous variables in the current expression
-  //! <symbolID, lag>
-  set< pair<int,int> > present_endogenous;
-
 public:
   ExprNode(DataTree &datatree_arg);
   virtual ~ExprNode();
@@ -143,16 +139,16 @@ public:
   //! Writes output of node (with no temporary terms and with "outside model" output type)
   void writeOutput(ostream &output);
 
-  //! Collects the Endogenous in a expression
-  virtual void collectEndogenous(NodeID &Id) = 0;
+  //! Computes the set of endogenous variables in the expression
+  /*! Endogenous are stored as integer pairs of the form (symb_id, lag)
+      They are added to the set given in argument */
+  virtual void collectEndogenous(set<pair<int, int> > &result) const = 0;
   virtual void computeTemporaryTerms(map<NodeID, int> &reference_count,
                                      temporary_terms_type &temporary_terms,
                                      map<NodeID, int> &first_occurence,
                                      int Curr_block,
                                      Model_Block *ModelBlock,
                                      map_idx_type &map_idx) const;
-  int present_endogenous_size() const;
-  int present_endogenous_find(int var, int lag) const;
 
   class EvalException
   {
@@ -182,7 +178,7 @@ private:
 public:
   NumConstNode(DataTree &datatree_arg, int id_arg);
   virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms) const;
-  virtual void collectEndogenous(NodeID &Id);
+  virtual void collectEndogenous(set<pair<int, int> > &result) const;
   virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
   virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
 };
@@ -201,7 +197,7 @@ private:
 public:
   VariableNode(DataTree &datatree_arg, int symb_id_arg, Type type_arg, int lag_arg);
   virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms = temporary_terms_type()) const;
-  virtual void collectEndogenous(NodeID &Id);
+  virtual void collectEndogenous(set<pair<int, int> > &result) const;
   virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
   virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
 };
@@ -226,7 +222,7 @@ public:
                                      int Curr_block,
                                      Model_Block *ModelBlock,
                                      map_idx_type &map_idx) const;
-  virtual void collectEndogenous(NodeID &Id);
+  virtual void collectEndogenous(set<pair<int, int> > &result) const;
   static double eval_opcode(UnaryOpcode op_code, double v) throw (EvalException);
   virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
   virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
@@ -253,7 +249,7 @@ public:
                                      int Curr_block,
                                      Model_Block *ModelBlock,
                                      map_idx_type &map_idx) const;
-  virtual void collectEndogenous(NodeID &Id);
+  virtual void collectEndogenous(set<pair<int, int> > &result) const;
   static double eval_opcode(double v1, BinaryOpcode op_code, double v2) throw (EvalException);
   virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
   virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
@@ -285,7 +281,7 @@ public:
                                      int Curr_block,
                                      Model_Block *ModelBlock,
                                      map_idx_type &map_idx) const;
-  virtual void collectEndogenous(NodeID &Id);
+  virtual void collectEndogenous(set<pair<int, int> > &result) const;
   static double eval_opcode(double v1, TrinaryOpcode op_code, double v2, double v3) throw (EvalException);
   virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
   virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
@@ -310,7 +306,7 @@ public:
                                      int Curr_block,
                                      Model_Block *ModelBlock,
                                      map_idx_type &map_idx) const;
-  virtual void collectEndogenous(NodeID &Id);
+  virtual void collectEndogenous(set<pair<int, int> > &result) const;
   virtual double eval(const eval_context_type &eval_context) const throw (EvalException);
   virtual void compile(ofstream &CompileCode, bool lhs_rhs, ExprNodeOutputType output_type, const temporary_terms_type &temporary_terms, map_idx_type map_idx) const;
 };