fix segv with threads (closes #17, closes #18)