Modified code to make the extension work properly with a control on number of threads spawned.