Added code to calculate and cache matrix inverse