Rewrite complete

But this needs testing