added another flag to track detach, as dtor will call detach again, plus some comments