A fair bit of work on database setup and templates

(Still not happy with how the database stuff is implemented.)