Refactor schema middleware handlers