Proper bounds check on heap index and queue size.