Fix tabs not being removed correctly.
Refactor some tab template stuff.