fix cost function