Fixing some thread related bugs
Thread work around