Defining the optimizer function