Partial rewrite, cleanup