refactored it to use NFA