added table rewrite and tests