Hi and thanks for your comment.
On second glance I realized that the whole while loop for the language code was crappy and I left it out altogether. It should work now (though not as nicely as I intended).
Oddly enough the undefunless typo only appeared when I posted the code.
As for the HTML thing, is there a module that can easily take care of that?
Thanks for your reply.