refactor networking code