Fixes and refactors log functionality

Fixes #4111