Added option to specify max number of threads