Added some template functionality. TODO: Refactor. This is a mess