Restructure pm script