Cleanup.
Fixed some regressions.
Removed code duplication.
Repaired testsuite.