Rewrite most innards, improve performance