Reorganize a bit.

Move helper functions up with the rest and handleProtocol down below.