Add a flag to make cleanup() idempotent