Cleaning up sql queries