Move communication stuff out of main

I'd make the rest of this function a method as well and run it as a goroutine.