More work on fixing the SQL