improve formatting of error messages and simplify error-handling code