move code out of templates - not sure how this even worked.