Refactored messaging logic. May still bugs