refactor toRoute function