Proper implementation of matrix multiplication