Add new test.
Factor out common logic into CanonicalNumberSystem.
Miscel refactorings and checks.