Made middleware more robust.