Inlined many of the encode functions