Fixed SQL queries bug