Threads algorithm