Insert code that was in core before. Improve regexes and matching to fix #1