Using select, fixed infinite loop bug