Bug fixes to allow it to work with gunicorn