implemented costfunction