Proper encode function