Modified to implement matrix solve() with caching