fixed writePlan function