Fixed invalid database queries