[issue-36] First pass at pulling logic out of the template and simplify ...

...some of the logic too