updated multithreaded version