Fixed bugs with templates