Refactored routing code. Much cleaner now.