Refactor middleware to extract pushing onto stack