moved cost calculation for MST into the if statement