cleaning query function