Fix a bunch of issues with the context cache and querying in general