Implemented the cached matrix