implement multithreading using thread pools. Fix problems with random wait function.