Fixed a lot of severe program flow issues.