Finish rewriting database.py