function to call right before optimization