implements middleware