Fixing the query implementation