Merged enrico-inprogress with anurag. Refactored some network code.