middleware.py cleaned up