Refactored messaging code a little