Refactored main loop.
Minor changes.