Rewrite

Time to put all these changes back into master!