Most of the work for #3. Really ugly, IMO, but now it's possible to have xml output.