mostly finished rewrite, do not use yet