fixed bugs at end switch