refactor api call, putting in functions