added cache and solve matrix function