re rewrite and fixes