Rewrite database.py from scratch