Move improvements to XNI back into master (#18)

While working on integrating XNI, we've found several bugs in XNI.  I'm
porting those back to the master branch.  Note that this doesn't use the
original patchset from the XNI branch because its got other changes in it
as well.  We can create a new remote tracking branch for using this version
of XNI with the test version as desired.