total refactor, replacing json templates with native php code