Added automatic thread pruning when necessary.