refactor middleware loader