Kjr/refactors

Opening this for comment/review.

All the commits should be pretty atomic.