Refactored message sending code into a common function