Refactor and hopefully fix http error catching - untested