Short comment: I see you use file position. It's not good.
Copy-paste from BDNZine (2006):
filePos is obsolete The entire filePos-based API is obsolete -- there is no good reason to use it for new code.
It exists for backwards compatibility only. Whenever you see a reference to
a filePos in your code, it is a sign that your code can be improved. See elementref.h
for an explanation of filePos vs. ElementRef, etc.
In general, new code should use the ElemHandle APIs, and it will be
simpler, faster, more reliable, and better.
In your code mdlSelect_addElementByRef seems to be the alternative.
With regards,
Jan