This seems to work as same as it works for Queue. Can we just reuse this code?