Move template listener code into templates

Closes #45