0.9.5/massive refactor

Merge refactor back in to 0.9.5 branch.