Refactor to namespace and create a token object
Fixes in encoding/decoding