Reduce complexity of event detection.
- Removes global variables isValid and queuedTag
- pushTag() function now just has one parameter, i.e. "the tag to push"
- Typeahead detection refactored into methods
- Add some utility methods to reduce duplicate code