Fixed some SQL queries