De-duped some cleanup logic.