updated cost function