A complete rewrite