almost finished rewrite