implemented multithreaded version