Made query more robust