implemented some logics in match function