completed code for cachematrix