Added hasChildren check to query method