Fixed a query clean up bug