Solved error with encode

Solved error with encode. "enconde" was outside the scope of the function.