Move the error catching to the right place.