Refactoring toward #258. Closes #259.
Merge.