Refactor network collection code