Huge code cleanup