Improved regexes a bit, made less terrible - it now works okay for simple templates.