Move all the threads related stuff to a separate module