Fix bugs when calculating routes