Fixed some bugs in the routing code