final code for cachematrix