Reorganized code to have all messages flow through a single function that also handles the database entry.