Implemented caching for inversion calculation to reduce computation time when run repeatedly