CachedQuery code cleaned