split Encode into two public funcs to make it more flexible