Reworked the logical structure of db.py in preparations for the PHP replacement. Also fixed a spelling error in issue #5