cleaning up templates and refactoring JS