Add threading support

* TODO: It's not thread safe yet