Huge code cleanup in main class