Proposed rewrite

Here are my changes so far. Note: I haven't tested any of this yet so beware. I just opened this PR for discussion at least until the code is tested.