Fixed up sql queries