implemented cost function