refactor code for getUpstreams