Error handling.

seems a little odd that `returnedDoc` goes from a string to a DOM tree (line 103). Need a different variable?