Added null checking to switchUnitState