compute better cost function