correctly split range between threads. also allow any number of threads