non-destructive refactor into classes

TODO: this is a rebase but it has not been edited to track functionality
changed in -master.

Change-Id: I9687ea076a5e0b3d3f824793574b89ba22123c39