added check for inversible matrix