more rewrite. not done yet