compelte rewrite