Added code to calculate inverse matrix and cache