restructured to allow for running with gunicorn