Final code for decoding (both versions)