Reorganized code structure, cleaned up main()

Reorganized code structure, cleaned up main(), fixed some small issues
in documentation and other areas of the code.