The algorithm works except for targets.size()