Resolve many small bugs with the database.

Refactoring continues >_>