Completed inverting/caching code