rewrite and fixes