Fix issue with orphan queries