made the queries more versatile

The common parts of the query grouped together