Refactor code to deal with @robert-wf's suggestions

* Try/Catch clause placed around urlfetch
* Repetative code refactored