Move communication stuff out of main

I'd make this a method.