Added the use of databae wrapper to insert messages as well as other refactoring.