Refactored code to make UDP implementation easier.