some simplification and splitting out of functions in an attempt to reduce more of the code complexity