Refactor and updated to use with openfire.