Added body of matrix caching functions