Replace with rewrite branch

Rewritten from scratch, basically completely different