Improved logging and catching of errors.