added cost function