Handle errors more gracefully, credit: Philartphoto