more or less done with rewrite, doing some minor debugging