Move operational code within middleware function