clean-up code
Implements multithreaded version