Improved routing logic