final code for MP