extract function to turn off $trace at the appropriate time