start of total rewrite