Cachematrix function