Working solution of cachematrix function