diff --git a/libcelt/cwrs.c b/libcelt/cwrs.c
index 8f35c85b0c1dc8e985c4d8c36fffe40ead267d1a..5540a2d56a08d1e3b9ce062eac8b18d57e9157a8 100644
--- a/libcelt/cwrs.c
+++ b/libcelt/cwrs.c
@@ -652,7 +652,7 @@ void get_required_bits(celt_int16 *_bits,int _n,int _maxk,int _frac){
   _bits[0]=0;
   if (_n==1)
   {
-    for (k=1;k<_maxk;k++)
+    for (k=1;k<=_maxk;k++)
       _bits[k] = 1<<_frac;
   }
   else {
diff --git a/libcelt/dump_modes.c b/libcelt/dump_modes.c
index 1a8ad537eb041d4927f9b94c31cceddb95d3605c..cb0432bf00c1a625bed3c5a3604becf06420ee69 100644
--- a/libcelt/dump_modes.c
+++ b/libcelt/dump_modes.c
@@ -108,6 +108,19 @@ void dump_modes(FILE *file, CELTMode **modes, int nb_modes)
       fprintf(file, "#endif\n");
       fprintf(file, "\n");
 
+      fprintf(file, "#ifndef DEF_PULSE_CACHE%d_%d\n", mode->Fs, mdctSize);
+      fprintf(file, "#define DEF_PULSE_CACHE%d_%d\n", mode->Fs, mdctSize);
+      fprintf (file, "static const celt_int16 cache_index%d_%d[%d] = {\n", mode->Fs, mdctSize, (mode->maxLM+2)*mode->nbEBands);
+      for (j=0;j<mode->nbEBands*(mode->maxLM+2);j++)
+         fprintf (file, "%d, ", mode->cache.index[j]);
+      fprintf (file, "};\n");
+      fprintf (file, "static const unsigned char cache_bits%d_%d[%d] = {\n", mode->Fs, mdctSize, mode->cache.size);
+      for (j=0;j<mode->cache.size;j++)
+         fprintf (file, "%d, ", mode->cache.bits[j]);
+      fprintf (file, "};\n");
+      fprintf(file, "#endif\n");
+      fprintf(file, "\n");
+
       fprintf(file, "static const CELTMode mode%d_%d_%d = {\n", mode->Fs, mdctSize, mode->overlap);
       fprintf(file, "0x%x,\t/* marker */\n", 0xa110ca7e);
       fprintf(file, INT32 ",\t/* Fs */\n", mode->Fs);
@@ -121,18 +134,6 @@ void dump_modes(FILE *file, CELTMode **modes, int nb_modes)
       fprintf(file, "eBands%d_%d,\t/* eBands */\n", mode->Fs, mdctSize);
       fprintf(file, "%d,\t/* nbAllocVectors */\n", mode->nbAllocVectors);
       fprintf(file, "allocVectors%d_%d,\t/* allocVectors */\n", mode->Fs, mdctSize);
-      fprintf(file, "NULL,\t/* bits */\n");
-      fprintf (file, "{ ");
-      for (k=0;(1<<k>>1)<=mode->nbShortMdcts;k++)
-      {
-         int mdctSize2 = mode->shortMdctSize;
-         if (k>=1)
-            mdctSize2 <<= k-1;
-         else
-            mdctSize2 >>= 1;
-         fprintf (file, "allocCache%d_%d, ", mode->Fs, mdctSize2);
-      }
-      fprintf (file, "}, /* _bits */\n");
 
       fprintf(file, "{%d, 0, 0, 0},\t", 2*mode->shortMdctSize*mode->nbShortMdcts);
       fprintf (file, "/* mdct */\n");
@@ -143,6 +144,8 @@ void dump_modes(FILE *file, CELTMode **modes, int nb_modes)
       fprintf(file, "%d,\t/* shortMdctSize */\n", mode->shortMdctSize);
       fprintf(file, "0,\t/* prob */\n");
       fprintf(file, "logN%d_%d,\t/* logN */\n", mode->Fs, mdctSize);
+      fprintf(file, "{%d, cache_index%d_%d, cache_bits%d_%d},\t/* cache */\n",
+            mode->cache.size, mode->Fs, mdctSize, mode->Fs, mdctSize);
       fprintf(file, "0x%x,\t/* marker */\n", 0xa110ca7e);
       fprintf(file, "};\n");
    }
diff --git a/libcelt/modes.c b/libcelt/modes.c
index dc78cc389a46955c31d713d4dc11c0133f0c9a15..da0173e0661bec9182debc72fd3b36e8bc744428 100644
--- a/libcelt/modes.c
+++ b/libcelt/modes.c
@@ -297,7 +297,6 @@ CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error)
    if (mode==NULL)
       goto failure;
    CELT_COPY(mode, m, 1);
-   mode->bits = mode->_bits+1;
    mode->marker_start = MODEPARTIAL;
 #else
    int res;
@@ -424,7 +423,7 @@ CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error)
    compute_pulse_cache(mode, mode->maxLM);
 #endif /* !STATIC_MODES */
 
-   clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts, LM);
+   clt_mdct_init(&mode->mdct, 2*mode->shortMdctSize*mode->nbShortMdcts, mode->maxLM);
    if ((mode->mdct.trig==NULL)
 #ifndef ENABLE_TI_DSPLIB55
          || (mode->mdct.kfft==NULL)
@@ -476,8 +475,8 @@ void celt_mode_destroy(CELTMode *mode)
    celt_free((celt_word16*)mode->window);
    celt_free((celt_int16*)mode->logN);
 
-   celt_free(mode->cache.index);
-   celt_free(mode->cache.bits);
+   celt_free((celt_int16*)mode->cache.index);
+   celt_free((unsigned char*)mode->cache.bits);
 #endif
    clt_mdct_clear(&mode->mdct);
 
diff --git a/libcelt/modes.h b/libcelt/modes.h
index e45c542937ea0ca99f150acfc4fb3c6d9608d37d..226842a207304af295d338bab09ac5a05b33ccde 100644
--- a/libcelt/modes.h
+++ b/libcelt/modes.h
@@ -68,9 +68,9 @@
 #endif
 
 typedef struct {
-   int nbBands;
-   celt_int16 *index;
-   unsigned char *bits;
+   int size;
+   const celt_int16 *index;
+   const unsigned char *bits;
 } PulseCache;
 
 /** Mode definition (opaque)
diff --git a/libcelt/rate.c b/libcelt/rate.c
index eef27ea4e53337b6b22192a9b053f0db397c52c5..4835bccfb2742233b4d2d76ee01a0bbbe163a0f8 100644
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -75,32 +75,33 @@ void compute_pulse_cache(CELTMode *m, int LM)
    int entryN[100], entryK[100], entryI[100];
    const celt_int16 *eBands = m->eBands;
    PulseCache *cache = &m->cache;
+   celt_int16 *cindex;
+   unsigned char *bits;
 
-   cache->nbBands = m->nbEBands;
-   cache->index = celt_alloc(sizeof(cache->index[0])*cache->nbBands*(LM+2));
+   cindex = celt_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2));
+   cache->index = cindex;
 
    for (i=0;i<=LM+1;i++)
    {
       int j;
-      for (j=0;j<cache->nbBands;j++)
+      for (j=0;j<m->nbEBands;j++)
       {
          int k;
          int N = (eBands[j+1]-eBands[j])<<i>>1;
-         cache->index[i*cache->nbBands+j] = -1;
+         cindex[i*m->nbEBands+j] = -1;
          for (k=0;k<=i;k++)
          {
             int n;
-            for (n=0;n<cache->nbBands && (k!=i || n<j);n++)
+            for (n=0;n<m->nbEBands && (k!=i || n<j);n++)
             {
                if (N == (eBands[n+1]-eBands[n])<<k>>1)
                {
-                  cache->index[i*cache->nbBands+j] =
-                        cache->index[k*cache->nbBands+n];
+                  cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n];
                   break;
                }
             }
          }
-         if (cache->index[i*cache->nbBands+j] == -1)
+         if (cache->index[i*m->nbEBands+j] == -1 && N!=0)
          {
             int K;
             entryN[nbEntries] = N;
@@ -108,7 +109,7 @@ void compute_pulse_cache(CELTMode *m, int LM)
             while (fits_in32(N,get_pulses(K+1)) && K<MAX_PSEUDO-1)
                K++;
             entryK[nbEntries] = K;
-            cache->index[i*cache->nbBands+j] = curr;
+            cindex[i*m->nbEBands+j] = curr;
             entryI[nbEntries] = curr;
 
             curr += K+1;
@@ -116,11 +117,13 @@ void compute_pulse_cache(CELTMode *m, int LM)
          }
       }
    }
-   cache->bits = celt_alloc(sizeof(unsigned char)*curr);
+   bits = celt_alloc(sizeof(unsigned char)*curr);
+   cache->bits = bits;
+   cache->size = curr;
    for (i=0;i<nbEntries;i++)
    {
       int j;
-      unsigned char *ptr = cache->bits+entryI[i];
+      unsigned char *ptr = bits+entryI[i];
       celt_int16 tmp[MAX_PULSES];
       get_required_bits(tmp, entryN[i], get_pulses(entryK[i]), BITRES);
       for (j=1;j<=entryK[i];j++)
diff --git a/libcelt/rate.h b/libcelt/rate.h
index ebda8f325f1df29fe48424755149b6a7d4de7549..aa6b6408ede6be212d1abc4a534373f1d2c6293e 100644
--- a/libcelt/rate.h
+++ b/libcelt/rate.h
@@ -59,10 +59,10 @@ static inline int bits2pulses(const CELTMode *m, int band, int LM, int bits)
 {
    int i;
    int lo, hi;
-   unsigned char *cache;
+   const unsigned char *cache;
 
    LM++;
-   cache = m->cache.bits + m->cache.index[LM*m->cache.nbBands+band];
+   cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
 
    lo = 0;
    hi = cache[0];
@@ -84,10 +84,10 @@ static inline int bits2pulses(const CELTMode *m, int band, int LM, int bits)
 
 static inline int pulses2bits(const CELTMode *m, int band, int LM, int pulses)
 {
-   unsigned char *cache;
+   const unsigned char *cache;
 
    LM++;
-   cache = m->cache.bits + m->cache.index[LM*m->cache.nbBands+band];
+   cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
    return pulses == 0 ? 0 : cache[pulses]+1;
 }