fixed queue_length function