decode: restructure the core decoding code