Refactor udp code