Rweriting the whole query