Huge rewrite

Merge to myself.