xml write refactored and working