Fix bugs, improve logging-related code

Fixes #156, fixes #157, fixes #158