HUGE CODE REFACTOR