This is first cost function which sometimes works