Refactoring to use  to stack in middleware