Fixed while loop for threading, now exits gracefully