Cleanup and optimization on network generation