updated to include inverting of a matrix