Big rewrite! Better setup for database use.