refine the implementation of subapp as middleware