Refactored middleware.py to state of whydah-2.0.alfa15