Fixed thread safety problem, added better performance test and stats