Moving ot new middleware and reducing duplicate code