updated x.match(y) to y.test(x) and minor refactoring