Refactor, improve regexps, handle plain TeX