working version of cached matrix