Moved database connection into it's own function as per http://flask.pocoo.org/docs/tutorial/dbcon/.