Seperated the encode/decode elements

Bugs in variable scope