Extract middleware functionality to an external class