deal with complex encoding patters during collapsing

@jeremy