Improve pattern matching (template mode deprecated as of Dec 2018)