centralize tear-down operations and move them into a critical section