Changed the matrix functions to return a matrix, everything works