Modified the code to add the cached inverse functionality