modification of both functions to cache matrix inversion