rewriting the queries