fix thread storage and add tests