Further improvements to the optimizer function.