I think you can just use firstElementChild here to ignore text nodes and comments. Then you don't need the loop below.