Refactoring (issue #24)

This is partial work. More to come.