improve/fix template class bugs