(chore) Fix in middleware to check env setup in backend, other minor refactor