Refactor, reorganize, and clean up XMPP code