Multithreaded issues

Assertions could previously fail.
Reformatted code