Refactor middleware to take the client on its constructor