implemented cachematrix code