--- a/mex/comp_filterbank_td.c
+++ b/mex/comp_filterbank_td.c
@@ -45,7 +45,7 @@
 
 /* Obtain this filename. */
 #if defined(__GNUC__) || defined(__ICC)
-  #define MEX_FILE __BASE_FILE__
+#define MEX_FILE __BASE_FILE__
 #endif
 #include "ltfat_mex_template_helper.h"
 
@@ -70,108 +70,54 @@
 %         c  : Cell array of length M. Each element is N(m)*W array.
 */
 
-void LTFAT_NAME(tdMexAtExitFnc)()
+void
+LTFAT_NAME(ltfatMexFnc)( int nlhs, mxArray *plhs[],
+                         int nrhs, const mxArray *prhs[] )
 {
-   #ifdef _DEBUG
-   mexPrintf("Exit fnc called: %s\n",__PRETTY_FUNCTION__);
-   #endif
-}
+    const mxArray* mxf = prhs[0];
+    const mxArray* mxg = prhs[1];
+    double* aDouble = (double*) mxGetData(prhs[2]);
+    double* offsetDouble = (double*) mxGetData(prhs[3]);
+    ltfatExtType ext = ltfatExtStringToEnum( mxArrayToString(prhs[4]) );
 
 
-void LTFAT_NAME(ltfatMexFnc)( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
-{
-  #ifdef _DEBUG
-  static int atExitFncRegistered = 0;
-  if(!atExitFncRegistered)
-  {
-     LTFAT_NAME(ltfatMexAtExit)(LTFAT_NAME(tdMexAtExitFnc));
-     atExitFncRegistered = 1;
-  }
-  #endif
+    // input data length
+    mwSize L = mxGetM(mxf);
+    // number of channels
+    mwSize W = mxGetN(mxf);
+    // filter number
+    mwSize M = mxGetNumberOfElements(mxg);
 
-  const mxArray* mxf = prhs[0];
-  const mxArray* mxg = prhs[1];
-  double* a = (double*) mxGetData(prhs[2]);
-  double* offset = (double*) mxGetData(prhs[3]);
-  char* ext = mxArrayToString(prhs[4]);
+    // POINTER TO THE INPUT
+    LTFAT_TYPE* fPtr =  mxGetData(prhs[0]);
+
+    // POINTER TO THE FILTERS
+    const LTFAT_TYPE* gPtrs[M];
+    ltfatInt filtLen[M];
+    ltfatInt a[M];
+    ltfatInt offset[M];
+
+    // POINTER TO OUTPUTS
+    LTFAT_TYPE* cPtrs[M]; // C99 feature
+    plhs[0] = mxCreateCellMatrix(M, 1);
+    for(mwIndex m=0; m<M; ++m)
+    {
+        a[m]= (ltfatInt) aDouble[m];
+        offset[m] = (ltfatInt) offsetDouble[m];
+        filtLen[m] = (ltfatInt) mxGetNumberOfElements(mxGetCell(mxg,m));
+        mwSize outLen = (mwSize) filterbank_td_size(L,a[m],filtLen[m],
+                                                offset[m],ext);
+        mxSetCell(plhs[0], m,
+                  ltfatCreateMatrix(outLen,
+                                    W,LTFAT_MX_CLASSID,
+                                    LTFAT_MX_COMPLEXITY));
+        cPtrs[m] = mxGetData(mxGetCell(plhs[0],m));
+        gPtrs[m] = mxGetData(mxGetCell(mxg, m));
+    }
 
 
-  // input data length
-  mwSize L = mxGetM(mxf);
-  // number of channels
-  mwSize W = mxGetN(mxf);
-  // filter number
-  mwSize M = mxGetNumberOfElements(mxg);
-  // filter lengths
-  mwSize filtLen[M];
-  //mwSize* filtLen = mxMalloc(M*sizeof(mwSize));
-  for(unsigned int m=0;m<M;m++)
-  {
-     filtLen[m] = (mwSize) mxGetNumberOfElements(mxGetCell(mxg,m));
-  }
+    LTFAT_NAME(filterbank_td)(fPtr,gPtrs,L,filtLen,W,a,offset,M,cPtrs,ext);
 
-  // output lengths
-  mwSize outLen[M];
-  //mwSize* outLen = mxMalloc(M*sizeof(mwSize));
-  if(!strcmp(ext,"per"))
-  {
-     for(unsigned int m = 0; m < M; m++)
-     {
-        outLen[m] = (mwSize) ceil( L/a[m] );
-     }
-  }
-  else if(!strcmp(ext,"valid"))
-  {
-     for(unsigned int m = 0; m < M; m++)
-     {
-        outLen[m] = (mwSize) ceil( (L-(filtLen[m]-1))/a[m] );
-     }
-  }
-  else
-  {
-     for(unsigned int m = 0; m < M; m++)
-     {
-        outLen[m] = (mwSize) ceil( (L + filtLen[m] - 1 + offset[m] )/a[m] );
-     }
-  }
-
-     // POINTER TO THE INPUT
-     LTFAT_TYPE* fPtr = (LTFAT_TYPE*) mxGetData(prhs[0]);
-
-     // POINTER TO THE FILTERS
-     LTFAT_TYPE* gPtrs[M];
-     // LTFAT_TYPE** gPtrs = (LTFAT_TYPE**) mxMalloc(M*sizeof(LTFAT_TYPE*));
-     for(mwIndex m=0;m<M;m++)
-     {
-        gPtrs[m] = (LTFAT_TYPE*) mxGetPr(mxGetCell(mxg, m));
-     }
-
-     // POINTER TO OUTPUTS
-     LTFAT_TYPE* cPtrs[M]; // C99 feature
-     //LTFAT_TYPE** cPtrs = (LTFAT_TYPE**) mxMalloc(M*sizeof(LTFAT_TYPE*));
-     plhs[0] = mxCreateCellMatrix(M, 1);
-     for(mwIndex m=0;m<M;++m)
-     {
-        mxSetCell(plhs[0], m, ltfatCreateMatrix(outLen[m], W,LTFAT_MX_CLASSID,LTFAT_MX_COMPLEXITY));
-        cPtrs[m] = (LTFAT_TYPE*) mxGetData(mxGetCell(plhs[0],m));
-        memset(cPtrs[m],0,outLen[m]*W*sizeof(LTFAT_TYPE));
-     }
-
-     // over all channels
-   //  #pragma omp parallel for private(m)
-
-        for(mwIndex m =0; m<M; m++)
-        {
-          for(mwIndex w =0; w<W; w++)
-          {
-           // Obtain pointer to w-th column in input
-           LTFAT_TYPE *fPtrCol = fPtr + w*L;
-           // Obtaing pointer to w-th column in m-th element of output cell-array
-           LTFAT_TYPE *cPtrCol = cPtrs[m] + w*outLen[m];
-           //conv_td_sub(fPtrCol,L,&cPtrCol,outLen[m],(const double**)&gPtrs[m],filtLen[m],1,a[m],skip[m],ltfatExtStringToEnum(ext),0);
-           LTFAT_NAME(convsub_td)(fPtrCol,L,cPtrCol,outLen[m],gPtrs[m],filtLen[m],a[m],-offset[m],ltfatExtStringToEnum(ext));
-          }
-        }
 }
 #endif