Finish the cachematrix function