Fixed middleware main function to allow actual distributivity.