Fixed #15053 -- Improved template extending algorithm.

Thanks. I've updated with these changes.