Refactor of packet handling state machine to be packet-driven

Here's a little stab at a refactor. I'd love to pair on this with you when we can run it on your machine :D

with @akmcc