Refactor for xhr requests