Refactoring router logic