Diversion Headers

Hello people,
I have a very strange using Freepbx 2.8.1.4.

I am using Asterisk/FreePBX as an IVR platform and i have the following scenario.
Unanswered incoming calls for users are diverted to asterisk server to be matched by an IVR. IVR options for the caller is to either call the desired user on their cell phone or leave a voicemail.
Routing to cell phones works ok but for voicemail I have an issue with redirection headers. Voicemail is hosted on another server and it matches the oldest redirection header it finds in the INVITE message to match the voicemail inbox.
Now some of the redirected calls inbound to Asterisk have 2 diversion headers due to the use of hunt pilots (one diversion header for the original called extension and one for the hunt pilot which the calls was forwarded to).
Problem is that after the IVR , the outgoing call initiated towards voicemail server has the first diversion header missing and the matching of the voicemail inbox fails.
I cannot find why and when the diversion header is being removed.
I paste output from * console for a sample call.

;Doing some processing before sending to IVR
– Executing [2004@from-trunk:1] Goto(“SIP/ikcmp-in-000000c8”, “ext-did-km-custom,2004,1”) in new stack
– Goto (ext-did-km-custom,2004,1)
– Executing [2004@ext-did-km-custom:1] Set(“SIP/ikcmp-in-000000c8”, “myVar=sip:[email protected];reason=no-answer;privacy=off;screen=yes,:,2)}”) in new stack
– Executing [2004@ext-did-km-custom:2] Set(“SIP/ikcmp-in-000000c8”, “cutVar=<sip:1991”) in new stack
– Executing [2004@ext-did-km-custom:3] NoOp(“SIP/ikcmp-in-000000c8”, “1991”) in new stack
– Executing [2004@ext-did-km-custom:4] Set(“SIP/ikcmp-in-000000c8”, “cutVar=1991”) in new stack
– Executing [2004@ext-did-km-custom:5] Set(“SIP/ikcmp-in-000000c8”, “__ORIGINALDNID=1991”) in new stack
– Executing [2004@ext-did-km-custom:6] NoOp(“SIP/ikcmp-in-000000c8”, “1991”) in new stack
– Executing [2004@ext-did-km-custom:7] Goto(“SIP/ikcmp-in-000000c8”, “ext-did-0002,2004,1”) in new stack
– Goto (ext-did-0002,2004,1)
– Executing [2004@ext-did-0002:1] Set(“SIP/ikcmp-in-000000c8”, “__FROM_DID=2004”) in new stack
– Executing [2004@ext-did-0002:2] Gosub(“SIP/ikcmp-in-000000c8”, “app-blacklist-check,s,1”) in new stack
– Executing [s@app-blacklist-check:1] GotoIf(“SIP/ikcmp-in-000000c8”, “0?blacklisted”) in new stack
– Executing [s@app-blacklist-check:2] Set(“SIP/ikcmp-in-000000c8”, “CALLED_BLACKLIST=1”) in new stack
– Executing [s@app-blacklist-check:3] Return(“SIP/ikcmp-in-000000c8”, “”) in new stack
– Executing [2004@ext-did-0002:3] ExecIf(“SIP/ikcmp-in-000000c8”, “1 ?Set(CALLERID(name)=2229)”) in new stack
– Executing [2004@ext-did-0002:4] Set(“SIP/ikcmp-in-000000c8”, “__CALLINGPRES_SV=allowed_not_screened”) in new stack
– Executing [2004@ext-did-0002:5] Set(“SIP/ikcmp-in-000000c8”, “CALLERPRES()=allowed_not_screened”) in new stack
– Executing [2004@ext-did-0002:6] Goto(“SIP/ikcmp-in-000000c8”, “ivr-3,s,1”) in new stack
– Goto (ivr-3,s,1)
– Executing [s@ivr-3:1] Set(“SIP/ikcmp-in-000000c8”, “MSG=custom/company-maria03”) in new stack
– Executing [s@ivr-3:2] Set(“SIP/ikcmp-in-000000c8”, “LOOPCOUNT=0”) in new stack
– Executing [s@ivr-3:3] Set(“SIP/ikcmp-in-000000c8”, “__DIR-CONTEXT=”) in new stack
– Executing [s@ivr-3:4] Set(“SIP/ikcmp-in-000000c8”, “_IVR_CONTEXT_ivr-3=”) in new stack
– Executing [s@ivr-3:5] Set(“SIP/ikcmp-in-000000c8”, “_IVR_CONTEXT=ivr-3”) in new stack
– Executing [s@ivr-3:6] GotoIf(“SIP/ikcmp-in-000000c8”, “0?begin”) in new stack
– Executing [s@ivr-3:7] Answer(“SIP/ikcmp-in-000000c8”, “”) in new stack
– Executing [s@ivr-3:8] Wait(“SIP/ikcmp-in-000000c8”, “1”) in new stack
– Executing [s@ivr-3:9] Set(“SIP/ikcmp-in-000000c8”, “TIMEOUT(digit)=3”) in new stack
– Digit timeout set to 3.000
– Executing [s@ivr-3:10] Set(“SIP/ikcmp-in-000000c8”, “TIMEOUT(response)=11”) in new stack
– Response timeout set to 11.000
– Executing [s@ivr-3:11] Set(“SIP/ikcmp-in-000000c8”, “__IVR_RETVM=”) in new stack
– Executing [s@ivr-3:12] ExecIf(“SIP/ikcmp-in-000000c8”, “1?Background(custom/company-maria03)”) in new stack
– <SIP/ikcmp-in-000000c8> Playing ‘custom/company-maria03.slin’ (language ‘en’)
– Executing [1@ivr-3:1] NoOp(“SIP/ikcmp-in-000000c8”, "Deleting: ") in new stack
– Executing [1@ivr-3:2] Set(“SIP/ikcmp-in-000000c8”, “__NODEST=”) in new stack

;sending to custom destination ext-KM-vm-custom which routes the call to Voicemail server

-- Executing [1@ivr-3:3] Goto("SIP/ikcmp-in-000000c8", "ext-KM-vm-custom,1,1") in new stack
-- Goto (ext-KM-vm-custom,1,1)
-- Executing [1@ext-KM-vm-custom:1] NoOp("SIP/ikcmp-in-000000c8", "Entering [ext-KM-vm-custom]") in new stack
-- Executing [1@ext-KM-vm-custom:2] Goto("SIP/ikcmp-in-000000c8", "ext-KM-vm-custom2,1,1") in new stack
-- Goto (ext-KM-vm-custom2,1,1)
-- Executing [1@ext-KM-vm-custom2:1] NoOp("SIP/ikcmp-in-000000c8", "Entering [ext-KM-vm-custom2]") in new stack
-- Executing [1@ext-KM-vm-custom2:2] Set("SIP/ikcmp-in-000000c8", "myVar=") in new stack
-- Executing [1@ext-KM-vm-custom2:3] Set("SIP/ikcmp-in-000000c8", "myVar=") in new stack
-- Executing [1@ext-KM-vm-custom2:4] Set("SIP/ikcmp-in-000000c8", "REDIRECTING(to-num,i)=2476") in new stack
-- Executing [1@ext-KM-vm-custom2:5] Set("SIP/ikcmp-in-000000c8", "REDIRECTING(count,i)=1") in new stack
-- Executing [1@ext-KM-vm-custom2:6] Set("SIP/ikcmp-in-000000c8", "REDIRECTING(reason,i)=cfu") in new stack
-- Executing [1@ext-KM-vm-custom2:7] Goto("SIP/ikcmp-in-000000c8", "outrt-4,2000,1") in new stack
-- Goto (outrt-4,2000,1)
-- Executing [2000@outrt-4:1] Macro("SIP/ikcmp-in-000000c8", "user-callerid,SKIPTTL,") in new stack
-- Executing [s@macro-user-callerid:1] Set("SIP/ikcmp-in-000000c8", "AMPUSER=2229") in new stack
-- Executing [s@macro-user-callerid:2] GotoIf("SIP/ikcmp-in-000000c8", "0?report") in new stack
-- Executing [s@macro-user-callerid:3] ExecIf("SIP/ikcmp-in-000000c8", "1?Set(REALCALLERIDNUM=2229)") in new stack
-- Executing [s@macro-user-callerid:4] Set("SIP/ikcmp-in-000000c8", "AMPUSER=") in new stack
-- Executing [s@macro-user-callerid:5] Set("SIP/ikcmp-in-000000c8", "AMPUSERCIDNAME=") in new stack
-- Executing [s@macro-user-callerid:6] GotoIf("SIP/ikcmp-in-000000c8", "1?report") in new stack
-- Goto (macro-user-callerid,s,10)
-- Executing [s@macro-user-callerid:10] GotoIf("SIP/ikcmp-in-000000c8", "1?continue") in new stack
-- Goto (macro-user-callerid,s,19)
-- Executing [s@macro-user-callerid:19] Set("SIP/ikcmp-in-000000c8", "CALLERID(number)=2229") in new stack
-- Executing [s@macro-user-callerid:20] Set("SIP/ikcmp-in-000000c8", "CALLERID(name)=2229") in new stack
-- Executing [s@macro-user-callerid:21] NoOp("SIP/ikcmp-in-000000c8", "Using CallerID "2229" <2229>") in new stack
-- Executing [2000@outrt-4:2] NoOp("SIP/ikcmp-in-000000c8", "Calling Out Route: 2000_Exchange") in new stack
-- Executing [2000@outrt-4:3] Set("SIP/ikcmp-in-000000c8", "INTRACOMPANYROUTE=YES") in new stack
-- Executing [2000@outrt-4:4] Set("SIP/ikcmp-in-000000c8", "MOHCLASS=default") in new stack
-- Executing [2000@outrt-4:5] Set("SIP/ikcmp-in-000000c8", "_NODEST=") in new stack
-- Executing [2000@outrt-4:6] Macro("SIP/ikcmp-in-000000c8", "record-enable,,OUT,") in new stack
-- Executing [s@macro-record-enable:1] GotoIf("SIP/ikcmp-in-000000c8", "1?check") in new stack
-- Goto (macro-record-enable,s,4)
-- Executing [s@macro-record-enable:4] ExecIf("SIP/ikcmp-in-000000c8", "1?MacroExit()") in new stack
-- Executing [2000@outrt-4:7] Macro("SIP/ikcmp-in-000000c8", "dialout-trunk,3,2000,") in new stack
-- Executing [s@macro-dialout-trunk:1] Set("SIP/ikcmp-in-000000c8", "DIAL_TRUNK=3") in new stack
-- Executing [s@macro-dialout-trunk:2] GosubIf("SIP/ikcmp-in-000000c8", "0?sub-pincheck,s,1") in new stack
-- Executing [s@macro-dialout-trunk:3] GotoIf("SIP/ikcmp-in-000000c8", "0?disabletrunk,1") in new stack
-- Executing [s@macro-dialout-trunk:4] Set("SIP/ikcmp-in-000000c8", "DIAL_NUMBER=2000") in new stack
-- Executing [s@macro-dialout-trunk:5] Set("SIP/ikcmp-in-000000c8", "DIAL_TRUNK_OPTIONS=tr") in new stack
-- Executing [s@macro-dialout-trunk:6] Set("SIP/ikcmp-in-000000c8", "OUTBOUND_GROUP=OUT_3") in new stack
-- Executing [s@macro-dialout-trunk:7] GotoIf("SIP/ikcmp-in-000000c8", "0?nomax") in new stack
-- Executing [s@macro-dialout-trunk:8] GotoIf("SIP/ikcmp-in-000000c8", "0?chanfull") in new stack
-- Executing [s@macro-dialout-trunk:9] GotoIf("SIP/ikcmp-in-000000c8", "1?skipoutcid") in new stack
-- Goto (macro-dialout-trunk,s,12)
-- Executing [s@macro-dialout-trunk:12] GosubIf("SIP/ikcmp-in-000000c8", "0?sub-flp-3,s,1") in new stack
-- Executing [s@macro-dialout-trunk:13] Set("SIP/ikcmp-in-000000c8", "OUTNUM=2000") in new stack
-- Executing [s@macro-dialout-trunk:14] Set("SIP/ikcmp-in-000000c8", "custom=SIP/ikcmp-out") in new stack
-- Executing [s@macro-dialout-trunk:15] ExecIf("SIP/ikcmp-in-000000c8", "0?Set(DIAL_TRUNK_OPTIONS=M(setmusic^default)tr)") in new stack
-- Executing [s@macro-dialout-trunk:16] Macro("SIP/ikcmp-in-000000c8", "dialout-trunk-predial-hook,") in new stack
-- Executing [s@macro-dialout-trunk-predial-hook:1] MacroExit("SIP/ikcmp-in-000000c8", "") in new stack
-- Executing [s@macro-dialout-trunk:17] GotoIf("SIP/ikcmp-in-000000c8", "0?bypass,1") in new stack
-- Executing [s@macro-dialout-trunk:18] GotoIf("SIP/ikcmp-in-000000c8", "0?customtrunk") in new stack
-- Executing [s@macro-dialout-trunk:19] Dial("SIP/ikcmp-in-000000c8", "SIP/ikcmp-out/2000,300,tr") in new stack

Any ideas/pointers would be deeply appreciated.

Thanks!

Just a small note:
I have just tried rerouting the call back to a sip trunk without any custom scripts or IVR in the way and still one of the two Diversion headers is removed on the outgoing leg.