First working version with reordering of rows and columns