refactoring and fixing of network code