Refactor using middleware model