fix bug in cost func