Main entry point and lots of bug fixes.