Repaired (tested with supplied XML sample)
some functionalities may have to be revisioned later