Thanks to DARKER I have an updated version.
It seems that in some case one of the value I was relying on was 0 when it was supposed to be an offset telling me the end of the code segment.
Well in that case I treat the end of the file as the end of the code segment and it seems to do the trick.
Enjoys.