write messages we send into db too.

And fix a few bugs