HUGE code refactor