Nearly complete rewrite