improved queue code