complete (but untested) decode function