Clean up some of the SQL-generation