refactors match logic