Make it work with threads