refactored optimise parfor code