Issue 720 app factories

This is a very big change in the code in order to fully support the Flask pattern of [Factories](http://flask.pocoo.org/docs/patterns/appfactories/).

The changes simplify the code a lot, as well as all the tests.