diff --git a/DataTree.cc b/DataTree.cc
index 24621f3341b1cb484be5fdf6bfbb23bf930cceb0..e6ae3fcde1931e6c4d121540a21d6e267f8cab14 100644
--- a/DataTree.cc
+++ b/DataTree.cc
@@ -596,3 +596,14 @@ DataTree::isSecondDerivExternalFunctionUsed(int symb_id) const
 
   return false;
 }
+
+int
+DataTree::minLagForSymbol(int symb_id) const
+{
+  int r = 0;
+  for (variable_node_map_t::const_iterator it = variable_node_map.begin();
+       it != variable_node_map.end(); ++it)
+    if (it->first.first == symb_id && it->first.second < r)
+      r = it->first.second;
+  return r;
+}
diff --git a/DataTree.hh b/DataTree.hh
index 648c62c7d4001f7534355db8e9244aa272d05fdf..814284a5462e555e6dcc397790d5a2db590093f2 100644
--- a/DataTree.hh
+++ b/DataTree.hh
@@ -208,6 +208,9 @@ public:
   bool isFirstDerivExternalFunctionUsed(int symb_id) const;
   //! Checks if a given second derivative external function is used somewhere in the data tree
   bool isSecondDerivExternalFunctionUsed(int symb_id) const;
+  //! Returns the minimum lag (as a negative number) of the given symbol in the whole data tree (and not only in the equations !!)
+  /*! Returns 0 if the symbol is not used */
+  int minLagForSymbol(int symb_id) const;
   //! Thrown when trying to access an unknown variable by deriv_id
   class UnknownDerivIDException
   {
diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc
index ceccb9733d8d79e81baac61fb2f5528d03dfefd4..e15f2724c92ea696919e72883c7ef6d4b563f499 100644
--- a/NumericalInitialization.cc
+++ b/NumericalInitialization.cc
@@ -193,21 +193,27 @@ HistValStatement::writeOutput(ostream &output, const string &basename) const
       const expr_t expression = it->second;
 
       SymbolType type = symbol_table.getType(symb_id);
-      if (type == eEndogenous && lag < 0)
-	{
-	  const int new_symb_id = symbol_table.searchAuxiliaryVars(symb_id,lag);
-	  if (new_symb_id != -1)
-	    {
-	      symb_id = new_symb_id;
-	      lag = 0;
-	    }
-	  else if (symbol_table.AuxVarsSize() > 0)
-	    {
-	      cerr << "Histval: this variable doesn't exist with such a lag in the model" << endl;
-	      exit(EXIT_FAILURE);
-	    }
-	    
-	}
+
+      // For a lag greater than 1, lookup for auxiliary variable
+      if ((type == eEndogenous || type == eExogenous) && lag < 0)
+        {
+          try
+            {
+              symb_id = symbol_table.searchAuxiliaryVars(symb_id, lag);
+              lag = 0;
+            }
+          catch (SymbolTable::SearchFailedException &e)
+            {
+              if (type == eEndogenous)
+                {
+                  cerr << "HISTVAL: internal error of Dynare, please contact the developers";
+                  exit(EXIT_FAILURE);
+                }
+              // We don't fail for exogenous, because they are not replaced by
+              // auxiliary variables in deterministic mode.
+            }
+        }
+
       int tsid = symbol_table.getTypeSpecificID(symb_id) + 1;
 
       if (type == eEndogenous)
diff --git a/ParsingDriver.cc b/ParsingDriver.cc
index f0652bebb57cef3f5dd751fee397591f21f70651..a719334afe9814fa3f40d341196cd1aaac5a988a 100644
--- a/ParsingDriver.cc
+++ b/ParsingDriver.cc
@@ -21,6 +21,7 @@
 #include <fstream>
 #include <iostream>
 #include <cassert>
+#include <sstream>
 
 #include "ParsingDriver.hh"
 #include "Statement.hh"
@@ -392,11 +393,18 @@ ParsingDriver::hist_val(string *name, string *lag, expr_t rhs)
   if (type != eEndogenous
       && type != eExogenous
       && type != eExogenousDet)
-    error("hist_val: " + *name + " should be an endogenous or exogenous variable");
+    error("histval: " + *name + " should be an endogenous or exogenous variable");
 
   int ilag = atoi(lag->c_str());
   pair<int, int> key(symb_id, ilag);
 
+  if (mod_file->dynamic_model.minLagForSymbol(symb_id) > ilag - 1)
+    {
+      ostringstream s;
+      s << ilag-1;
+      error("histval: variable " + *name + " does not appear in the model with the lag " + s.str() + " (see the reference manual for the timing convention in 'histval')");
+    }
+
   if (hist_values.find(key) != hist_values.end())
     error("hist_val: (" + *name + ", " + *lag + ") declared twice");
 
diff --git a/SymbolTable.cc b/SymbolTable.cc
index 7043de839d92ed0cc94065d8f06d1613115247c7..aba5c399c02b3e2adf9083a29a6645a6c3f59715 100644
--- a/SymbolTable.cc
+++ b/SymbolTable.cc
@@ -368,13 +368,13 @@ SymbolTable::addExpectationAuxiliaryVar(int information_set, int index, const st
 }
 
 int 
-SymbolTable::searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const
+SymbolTable::searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const throw (SearchFailedException)
 {
-  for (int i=0; i < aux_vars.size();++i)
+  for (size_t i = 0; i < aux_vars.size(); i++)
     if ((aux_vars[i].get_type() == avEndoLag || aux_vars[i].get_type() == avExoLag)
         && aux_vars[i].get_orig_symb_id() == orig_symb_id && aux_vars[i].get_orig_lead_lag() == orig_lead_lag)
       return aux_vars[i].get_symb_id();
-  return -1;
+  throw SearchFailedException(orig_symb_id, orig_lead_lag);
 }
 
 void
diff --git a/SymbolTable.hh b/SymbolTable.hh
index ad0579cdd686d39257f04e707b27b964a92a8408..0925c5eab4a61885e387e9e3f819d85153a621be 100644
--- a/SymbolTable.hh
+++ b/SymbolTable.hh
@@ -47,8 +47,8 @@ class AuxVarInfo
 private:
   int symb_id; //!< Symbol ID of the auxiliary variable
   aux_var_t type; //!< Its type
-  int orig_symb_id; //!< Symbol ID of the endo of the original model represented by this aux var. Not used for avEndoLead
-  int orig_lead_lag; //!< Lead/lag of the endo of the original model represented by this aux var. Not used for avEndoLead
+  int orig_symb_id; //!< Symbol ID of the endo of the original model represented by this aux var. Only used for avEndoLag and avExoLag.
+  int orig_lead_lag; //!< Lead/lag of the endo of the original model represented by this aux var. Only used for avEndoLag and avExoLag.
   string expectation_information_set_name; //!< Stores 'full' or 'varobs' for avExpectationRIS. Not used otherwise.
 public:
   AuxVarInfo(int symb_id_arg, aux_var_t type_arg, int orig_symb_id, int orig_lead_lag, string expectation_information_set_name_arg);
@@ -161,6 +161,16 @@ public:
   class NotYetFrozenException
   {
   };
+  //! Thrown when searchAuxiliaryVars() failed
+  class SearchFailedException
+  {
+  public:
+    int orig_symb_id, orig_lead_lag;
+    SearchFailedException(int orig_symb_id_arg, int orig_lead_lag_arg) : orig_symb_id(orig_symb_id_arg),
+                                                                         orig_lead_lag(orig_lead_lag_arg)
+    {
+    }
+  };
 
 private:
   //! Factorized code for adding aux lag variables
@@ -204,11 +214,13 @@ public:
     \return the symbol ID of the new symbol
   */
   int addExpectationAuxiliaryVar(int information_set, int index, const string &information_set_name) throw (FrozenException);
-  //! Searches auxiliary variables by symbol_id and lead_lag
+  //! Searches auxiliary variables which are substitutes for a given symbol_id and lead/lag
   /*!
-    \return the symbol ID of the auxiliary variable and -1 if not found
+    The search is only performed among auxiliary variables of endo/exo lag.
+    \return the symbol ID of the auxiliary variable
+    Throws an exception if match not found.
   */
-  int searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const;
+  int searchAuxiliaryVars(int orig_symb_id, int orig_lead_lag) const throw (SearchFailedException);
   //! Returns the number of auxiliary variables
   int AuxVarsSize() const {return aux_vars.size();};
   //! Tests if symbol already exists