Updated query function