fixed bugs in match function