simplified post-processing and fixed unmatched end problem