Mostly complete major rewrite