Restructured handle_datagram

Adjusted structure to handle more return cases as part of the same if-else chain and to use run_count more consistently throughout.