Fix infinite loops and some regular expressions