fix broken SQL query. Solves the last issue. Finally!