Updated cache matrix function