improve XML parsing and refactor