refactor

Safe, incremental code refactoring with test verification at each step.