moved search logic from the middleware into classes