Rewrite of core coder & decoder functions to fix several bugs and limitations, and improve error handling.