huge code cleanup