This commit attempts to mirror what the decoder does more closely in addition to fixing some shortcomings.