Refined the algorithm to find destination id