Fix the "Cyclomatic complexity exceeds 10" warning by simplifying encode(). Score: 10/10.