similarity function. TODO: stopping cond