Refactor XHR utils, remove code duplication