completed implementation of Refactor.
Some aspects could use another pass.
not tested.