refining app middleware implementation