Fixed a bug in the computation of the cost