Corrected Matrix inversion function