Rewrote pthread version