refactor class state into middleware environment