Refactoring and adding tests for route ordering