Fix template struct and some bugs

Fix template struct and some bugs.