Error messages

Improved error messages in document and image loops, so that the original error is logged.