Switched to mako templates and fixed a bunch of issues on the way