added lots of infrastructure and encode/decode stuff