Threading

Make code capable of multithreading.