rewrite feature complete, still needs testing