fix routing issue

This code is hard to read. Please improve. This code is similar to other code in this file. Please reuse code as much as possible.