Wrote functions for main().

Decomposed main() into functions; they are now written. Not tested to
see which give segfaults.