Start of near complete rewrite