Fixed the condition for the replace of the plan.
Improved automatic adjusting of plans, added unstaging for some cases.