Rough refactor for #22.