Threading cleanup

Adding threading cleanup when closing.