fixed template switches, memleaks and some indent