Improved error logging

Fixes #223