master middleware code