Restructing the code for multiprocessing