Since we're working with a stack (last in first out), we need to append to it in reverse to preserve the order when popped. Fix #11937.