refactoried match implementation