HUGE code refactoring