Edited for matrix inversion