Added response code check

Error response codes (e.g. 404) made the code break in an unlovely way.
To address this, I added a response code check.