Refactored some routing code