move writing network to its own function