Reworked packet handling to prevent memory leaks.