Deduplication of code in messages.cpp