Fix tbg

- fix that nested templates were not solved
- add variable cycle dependency detection