Re #1143 Refactoring and tests.