Removed some of the SQL duplication.