Move client and server side implementations into separate files to increase readability. Also get rid of a goto.