working multithreaded solution