'Functionalized' the program to split things up

I've made a start on making the code neater, by splitting up the
various parts of the code into function units. It's still not
completely refactored the way I would like, but I think it's better
than it was.
One Down side is that the code is no longer parallelizable.. neeed to
fix the loop so that the loop variable increments by 1 instead of
two... not too hard... but will leave till later.