remove infinite queues and put results extraction code in function form