Huge code overhaul