Refactored code

Moved each major step into a function.  Later will be used with timeit
to determine speed of different sort meathods.