Finally changed to app_factory pattern