refactoring for better compatibility with uwsgi and other application servers