added multithreading and more refactoring