Performed a lot of untested refactoring.

Implemented the needed scenario cpdh.
(scary! do not run)