Cleanup encode messages code