Replaced busy wait with condition wait in detection