Refactor cacheSolve function