rewrite plan code