Fix template replacement bug