Refactor routes with middleware