fixed threading related bugs