Changes according Dominic's review notes:
- use assert_unreached
- code formatting
- template elements test removed
- more informative variable names