added cached matrix implementation