significant refactoring, output is from toString method rather than XML