Refactors router; adds more tests.