Added proper termination.
Made the MPI code generic