beginning of optimization rewrite