Change database to use AUTOINCREMENT instead of a hacky guessing-the-next-row approach.
Also split template files up a bit to be more modular.