I haven't unraveled all the logic here, but certainly going from matching Ctx#ctx.doc to {ok, D} to matching {Ctx#ctx.doc, T} to {ok, D} has got to be wrong, right?