Implemented better handling of OpenMP threads