refactored the way matrixs are stored and constructed