Updated the function for matrix inversion