Changed to iterative method and added multithreading