more refactor on tree, mat and case