Logical Restructuring

Dividing the functions so they make more sense logically, small changes
in an attempt to get correct output.  No success so far.