Small clean-ups and leak fixes in the OPENDIR mechanism.

Wow, that was subtle! I wonder how it didn't trigger. Maybe just by coincidence ```end``` always gets the same place on the stack, so it ends up having the proper value... :-) I'll move the declaration outside the loop and rebase, thanks!