refactor code that make ttys in templates