       generateLongsFromGMP(a[j * nr_rows + i], tmpX[j * nr_rows + i], lengthOwnLimbs[j * nr_rows + i], ownLimbSize, maxExp); 
       if (minLengthOwnLimbs > lengthOwnLimbs[j * nr_rows + i]) minLengthOwnLimbs = lengthOwnLimbs[j * nr_rows + i];
     }
-
+  
   
   // Allocate the memory for the set of matrices (make all elements have the same length) such that 
   // elements of the matrices are closer in memory 
   // This might need to be rethough
-  x = (long long **) malloc(minLengthOwnLimbs * sizeof *x + (minLengthOwnLimbs * (nr_rows * nr_cols * sizeof **x)));
+  x = (long long **)malloc(minLengthOwnLimbs * sizeof *x);
+  for(int k = 0; k < sizeOfArray; k++)
+    x[k] = (long long *) malloc(nr_rows * nr_cols * sizeof **x);
   sizeOfArray = minLengthOwnLimbs;
   
   for(int k = 0; k < sizeOfArray; k++)
-    for(int i = 0; i < nr_rows; ++i)
-      for(int j = 0; j < nr_cols; ++j) {
+    for(int i = 0; i < nr_rows; i++)
+      for(int j = 0; j < nr_cols; j++) {
 	x[k][j * nr_rows + i] = tmpX[j * nr_rows + i][k];
       }
   free(tmpX);

// THIS HAS NOT BEEN TESTED YET
// TODO: decouple memory allocation from calculation eventually

void generateLongMatrixFromGMPMatrix(const mpf_class *a, const int nr_rows, const int nr_cols, long long **&x,  int &sizeOfArray, int &exp, const int ownLimbSize) {
  // Allocate memory for pointers that point to each element in matrix and to the array 
  // that gives the number of own limbs for each matrix element
  long long **tmpX;
  int * lengthOwnLimbs; 
  tmpX = (long long **) malloc( nr_rows * nr_cols * sizeof( long * ));
  lengthOwnLimbs = (int *) malloc( nr_rows * nr_cols * sizeof(int));


#include <sys/time.h>
#include "omp.h"
#include <bitset>
#include <math.h>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/io.hpp> 
