Fix custom templates (issue #256)

This should fix #256: it pretty much just makes the small adjustments suggested by others in that issue, and adds a couple integration tests for this.