Refactor routing code