Fixed some syntax errors, improved the query