That non-optional ENDIF to complete the structure might have something to do with it?

I do not see it in your code.