finished the main. Have not really tested.

Will test after removing debugging statements.