Fix bug in selecting cost function