Implemented most of match function logics