Closed #21 by adding exit case to thread loop