[QE-1103] Improvements to cleanup logic