Clean up packet handler and implement proper protocol state security