Fix typo.
Protect log by mutex and call only from master.
Refactor and improve error messages.