Start of implementing thread functions. This is a large chunk of irreducable complexity.