This should all go into a helper function like errorHandler or something.