Cleaned up the SQL queries a bit