Modified query function