Seperate middleware logics to their own function