add caching of matrix