slight optimization to pthread version