Much cleaner code for get_middleware()