Fixed error while calculating cost