updated cleanup logic