Refactoring code for enabling multiprocessing