fixed problems with "RuntimeError: working outside of application context"