added decode functions and part of encode (neither function has been
tested)