Refactored logic for checking for word boundaries.