Refactor of javascript, templates, a few db functions