Logic error when switched if statement