Fixed bug in cost fn calc