Added threading to algorithm