Refactor some key functions(matching, cps iteration)