Partial refactoring (TODO: refactor networking)