refactor middleware
add middleware