refactor options middlewares and how they are handled