Added code to invert and cache