Clean up threads properly with exit statements, fixes #218