optimized thread handling