Reworked most of the database-setup code.