Refactor network code