Code cleanup, and fix some unsafe pointer handling in Network.