Handle encoding errors more gracefully.