Using hashsets increased speed by factor of 8