improved the multithreading part of the code