Almost complete rewrite