diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index 515304ba1e09718fd3918391459e69c3a91ee461..a6d334dcfd0785262aeb2805dfcf4e8382f6aae8 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -34,6 +34,11 @@
 DynamicModel::DynamicModel(SymbolTable &symbol_table_arg,
                            NumericalConstants &num_constants_arg) :
   ModelTree(symbol_table_arg, num_constants_arg),
+  var_endo_nbr(0),
+  max_lag(0), max_lead(0),
+  max_endo_lag(0), max_endo_lead(0),
+  max_exo_lag(0), max_exo_lead(0),
+  max_exo_det_lag(0), max_exo_det_lead(0),
   cutoff(1e-15),
   markowitz(0.7),
   computeJacobian(false),
@@ -1820,14 +1825,14 @@ DynamicModel::writeOutput(ostream &output) const
      The matrix elements are equal to zero if a variable isn't present in the
      model at a given period.
   */
+
   output << "M_.lead_lag_incidence = [";
   // Loop on endogenous variables
-  int lag = 0;
   for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
     {
       output << endl;
       // Loop on periods
-      for (lag = -max_endo_lag; lag <= max_endo_lead; lag++)
+      for (int lag = -max_endo_lag; lag <= max_endo_lead; lag++)
         {
           // Print variableID if exists with current period, otherwise print 0
           try
diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc
index f91797f251e4858f5f2019a91be5e2740ad9cd2f..8cc372575726881891eb5be5c1e47db03984b6c5 100644
--- a/preprocessor/StaticModel.cc
+++ b/preprocessor/StaticModel.cc
@@ -139,12 +139,12 @@ StaticModel::writeStaticModel(ostream &StaticOutput) const
        it != first_derivatives.end(); it++)
     {
       int eq = it->first.first;
-      int var = it->first.second;
+      int symb_id = inv_deriv_id_table[it->first.second];
       NodeID d1 = it->second;
 
       ostringstream g1;
       g1 << "  g1";
-      matrixHelper(g1, eq, var, output_type);
+      matrixHelper(g1, eq, symbol_table.getTypeSpecificID(symb_id), output_type);
 
       jacobian_output << g1.str() << "=" << g1.str() << "+";
       d1->writeOutput(jacobian_output, output_type, temporary_terms);
@@ -157,12 +157,15 @@ StaticModel::writeStaticModel(ostream &StaticOutput) const
          it != second_derivatives.end(); it++)
       {
         int eq = it->first.first;
-        int var1 = it->first.second.first;
-        int var2 = it->first.second.second;
+        int symb_id1 = inv_deriv_id_table[it->first.second.first];
+        int symb_id2 = inv_deriv_id_table[it->first.second.second];
         NodeID d2 = it->second;
 
-        int col_nb = var1*symbol_table.endo_nbr()+var2;
-        int col_nb_sym = var2*symbol_table.endo_nbr()+var1;
+        int tsid1 = symbol_table.getTypeSpecificID(symb_id1);
+        int tsid2 = symbol_table.getTypeSpecificID(symb_id2);
+
+        int col_nb = tsid1*symbol_table.endo_nbr()+tsid2;
+        int col_nb_sym = tsid2*symbol_table.endo_nbr()+tsid1;
 
         hessian_output << "  g2";
         matrixHelper(hessian_output, eq, col_nb, output_type);
@@ -171,7 +174,7 @@ StaticModel::writeStaticModel(ostream &StaticOutput) const
         hessian_output << ";" << endl;
 
         // Treating symetric elements
-        if (var1 != var2)
+        if (symb_id1 != symb_id2)
           {
             lsymetric <<  "  g2";
             matrixHelper(lsymetric, eq, col_nb_sym, output_type);
@@ -282,23 +285,35 @@ StaticModel::computingPass(bool no_tmp_terms)
 int
 StaticModel::computeDerivID(int symb_id, int lag)
 {
-  if (symbol_table.getType(symb_id) == eEndogenous)
-    return symbol_table.getTypeSpecificID(symb_id);
-  else
+  // Only create derivation ID for endogenous
+  if (symbol_table.getType(symb_id) != eEndogenous)
     return -1;
+
+  deriv_id_table_t::const_iterator it = deriv_id_table.find(symb_id);
+  if (it != deriv_id_table.end())
+    return it->second;
+
+  // Create a new deriv_id
+  int deriv_id = deriv_id_table.size();
+
+  deriv_id_table[symb_id] = deriv_id;
+  inv_deriv_id_table.push_back(symb_id);
+
+  return deriv_id;
 }
 
 int
 StaticModel::getDerivID(int symb_id, int lag) const throw (UnknownDerivIDException)
 {
-  if (symbol_table.getType(symb_id) == eEndogenous)
-    return symbol_table.getTypeSpecificID(symb_id);
-  else
+  deriv_id_table_t::const_iterator it = deriv_id_table.find(symb_id);
+  if (it == deriv_id_table.end())
     throw UnknownDerivIDException();
+  else
+    return it->second;
 }
 
 int
 StaticModel::getDerivIDNbr() const
 {
-  return symbol_table.endo_nbr();
+  return deriv_id_table.size();
 }
diff --git a/preprocessor/StaticModel.hh b/preprocessor/StaticModel.hh
index 4f42185b9472083d289440c3c32831a9cfb56fe6..407e3b53243ceb1f7e7b25c2bfab979e776c480f 100644
--- a/preprocessor/StaticModel.hh
+++ b/preprocessor/StaticModel.hh
@@ -28,6 +28,12 @@ using namespace std;
 class StaticModel : public ModelTree
 {
 private:
+  typedef map<int, int> deriv_id_table_t;
+  //! Maps a symbol ID to a derivation ID
+  deriv_id_table_t deriv_id_table;
+  //! Maps a derivation ID to a symbol ID
+  vector<int> inv_deriv_id_table;
+
   //! Writes the static model equations and its derivatives
   /*! \todo handle hessian in C output */
   void writeStaticModel(ostream &StaticOutput) const;