DRY - extract some logic into middlewares