Several API, model and general design fixes

This pull request:

* Moves the application's `db` object to the main package, out of `models`.
* Fixes #297
* Fixes #298
* Fixes #299