Fix issues discovered by Coverity

Fix some issues that were discovered by Coverity.
Mainly fixes crashes when the elements in the XML file were ordered differently.