optimize code added a switch statement