finished optimizer code