Improved implementation of matrices