adding cost function