clean up middleware code