Networking refactoring

refactored networking and added a lot of error checking.