Optimized routing algorithm