move middlewares into lib