Wow, this looks messy

Maybe this is such a mess because I committed my work from a very old version of XChange? Anyway, let me know if I should do something else for a pull request.