Initial adaptation of code to use the ORM rather than raw queries (so that we're not limited to MySQL/SQLLite). Efforts have been made to make the use of the DB as efficient as possible.