Reorganize router code