Networking refactoring

some refactoring of networking. Still needs more bufixes