Refactor the packet handler to make it easier to handle multiple packets.