Use thread local storage instead of class variable

Closes #2