optimized Tdone function